import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: syyyy
 * Date: 2025-11-18
 * Time: 20:38
 */
public class TEst {
    public static String convert(String s,int numRows){
        int len=s.length();
        int numCols=len%(numRows+(numRows-2));//以一个V形作为一个单元
        if(numCols != 0){
            numCols=len/(numRows+(numRows-2))*2+1;
        }else {
            numCols=len/(numRows+(numRows-2))*2;
        }
        char[][] chars=new char[numRows][numCols];//列一定要初始化，或者延迟初始化
        int row=0;//表示行
        int col=0;//表示列
        for (int i = 0; i < len; i++){
            char ch=s.charAt(i);
            if(row != numRows && (col % (numRows-1) == 0)){
                chars[row][col]=ch;
                row++;
            }else {
                if(row == numRows) {
                    row = row - 2;//除了-1还要再减去最后一次row++的那个1，这一步只需要进行一次
                }else {
                    row--;
                }
                col++;
                chars[row][col]=ch;
            }

        }
        StringBuffer stringBuffer=new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            for (int j = 0; j < chars[i].length; j++) {
                char ch=chars[i][j];
                if(ch == '\u0000'){
                    continue;
                }else {
                    stringBuffer.append(ch);
                }
                stringBuffer.append(ch);
            }
        }
        return stringBuffer.toString();
    }

    public static void main(String[] args) {
        String ret=convert("PAYPALISHIRING",3);
        System.out.println(ret);
    }
}
